package cvi.com.reader.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.hawk.Hawk;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.activity.AuditionActivity;
import cvi.com.reader.activity.PersonalActivity;
import cvi.com.reader.activity.PictureBookAuditionActivity;
import cvi.com.reader.bean.ChartDataBean;
import cvi.com.reader.bean.constant;
import cvi.com.reader.tool.UtilsOnclickListen;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 梅涛 on 2020/7/24.
 */

public class ChartListAdapter extends BaseAdapter {
    private Context context;
    private List<ChartDataBean.DataBean> dataBeanList;
    private AuditionClickInterface auditionClickInterface;

    public AuditionClickInterface getAuditionClickInterface() {
        return auditionClickInterface;
    }

    public void setAuditionClickInterface(AuditionClickInterface auditionClickInterface) {
        this.auditionClickInterface = auditionClickInterface;
    }

    public ChartListAdapter(Context context, List<ChartDataBean.DataBean> dataBeanList) {
        this.context = context;
        this.dataBeanList = dataBeanList;
    }

    @Override
    public int getCount() {
        return dataBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viHolder = null;
        ChartDataBean.DataBean dataBean = dataBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_chart, null);
            viHolder = new ViewHolder(view);

            view.setTag(viHolder);
        } else {
            viHolder = (ViewHolder) view.getTag();
        }
        if (i == 0) {
            viHolder.num_ima.setVisibility(View.VISIBLE);
            viHolder.num_ima.setImageResource(R.mipmap.num1);
        } else if (i == 1) {
            viHolder.num_ima.setVisibility(View.VISIBLE);
            viHolder.num_ima.setImageResource(R.mipmap.num2);
        } else if (i == 2) {
            viHolder.num_ima.setVisibility(View.VISIBLE);
            viHolder.num_ima.setImageResource(R.mipmap.num3);
        } else {
            viHolder.num_ima.setVisibility(View.GONE);

        }
        ChartDataBean.DataBean.UserBean userBean = dataBean.getUser();
        if (userBean != null) {
            viHolder.class_name.setVisibility(View.VISIBLE);
            viHolder.name.setText(dataBean.getUser().getRealname());
            if (dataBean.getUser().getClassX() != null) {
                viHolder.class_name.setVisibility(View.VISIBLE);
                viHolder.class_name.setText(dataBean.getUser().getClassX());
            } else {
               // viHolder.class_name.setVisibility(View.GONE);
            }

            Glide.with(context).load("http://" + constant.URL + dataBean.getUser().getAvatar()).into(viHolder.imageview);
        }
        viHolder.num.setText((i + 1) + ".");

        viHolder.preaseNum.setText(dataBean.getScore() + "分");
        viHolder.auditBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    // 进行点击事件后的逻辑操作
                    Hawk.put("ChartDataBean", dataBean);
                    //  素材类型 0:文本类型 、1:绘本类型、2:影视类型
                    if ("0".equals(dataBean.getMaterial().getType())) {
                        Intent intent = new Intent(context, AuditionActivity.class);
                        context.startActivity(intent);
                    } else if ("1".equals(dataBean.getMaterial().getType())) {
                        Intent intent = new Intent(context, PictureBookAuditionActivity.class);
                        context.startActivity(intent);
                    }
                }


            }
        });
        return view;
    }

    public interface AuditionClickInterface {
        public void onAudition(String url);
    }

    static class ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.prease_num)
        TextView preaseNum;
        @BindView(R.id.audit_btn)
        LinearLayout auditBtn;
        @BindView(R.id.imageview)
        CircleImageView imageview;
        @BindView(R.id.num)
        TextView num;
        @BindView(R.id.class_name)
        TextView class_name;
        @BindView(R.id.num_ima)
        ImageView num_ima;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
